﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _2QWebProxy.App_Code;

namespace _2QWebProxy.GUI
{
    public partial class ucRuleSetting : UserControl
    {
        public ucRuleSetting()
        {
            InitializeComponent();
            if (GlobalVariables.listPriorityRule.Count == 0)
            {
                var listOfRules = Enum.GetValues(typeof(GlobalVariables.RuleType));
                foreach (var rule in listOfRules)
                {
                    GlobalVariables.listPriorityRule.Add(0);
                }
            }
            chkDenyFileType.Checked = Properties.Settings.Default.isDenyFileType;
            chkDenyIPAddress.Checked = Properties.Settings.Default.isDenyIPAddress;
            chkDenyWebsite.Checked = Properties.Settings.Default.isDenyWebsite;
            chkAllowIPAddress.Checked = Properties.Settings.Default.isAllowIPAddress;
            chkAllowWebsite.Checked = Properties.Settings.Default.isAllowWebsite;
            chkDenyFileTypeDomain.Checked = Properties.Settings.Default.isWebsiteDenyFileType;
            chkDenyFileTypeIPAddress.Checked = Properties.Settings.Default.isIPAddressDenyFileType;
        }

        private void ClearOtherDenyCheckBox(ref CheckBox checkBox)
        {
            chkDenyFileType.Checked = false;
            chkDenyIPAddress.Checked = false;
            chkDenyWebsite.Checked = false;
            checkBox.Checked = true;
        }

        private void ClearOtherAllowCheckBox(ref CheckBox checkBox)
        {
            chkAllowFileType.Checked = false;
            chkAllowIPAddress.Checked = false;
            chkAllowWebsite.Checked = false;
            checkBox.Checked = true;
        }

        private void chkAllowWebsite_Click(object sender, EventArgs e)
        {
            if (chkAllowWebsite.Checked)
            {
                ClearOtherDenyCheckBox(ref chkAllowWebsite);
            }
            Properties.Settings.Default.isAllowWebsite = chkAllowWebsite.Checked;
            CheckIsRule();
        }

        private void CheckIsRule()
        {
            Properties.Settings.Default.isRule = chkDenyFileType.Checked || chkDenyIPAddress.Checked || chkDenyWebsite.Checked || chkAllowIPAddress.Checked || chkAllowWebsite.Checked || chkDenyFileTypeDomain.Checked || chkDenyFileTypeIPAddress.Checked;
        }

        private void chkAllowIPAddress_Click(object sender, EventArgs e)
        {
            if (chkAllowIPAddress.Checked)
            {
                ClearOtherDenyCheckBox(ref chkAllowIPAddress);
            }
            Properties.Settings.Default.isAllowIPAddress = chkAllowIPAddress.Checked;
            CheckIsRule();
        }

        private void chkAllowFileType_Click(object sender, EventArgs e)
        {
            if (chkAllowFileType.Checked)
            {
                ClearOtherDenyCheckBox(ref chkAllowFileType);
            }
        }

        private void chkDenyFileType_Click(object sender, EventArgs e)
        {
            if (chkDenyFileType.Checked)
            {
                ClearOtherAllowCheckBox(ref chkDenyFileType);
            }
            Properties.Settings.Default.isDenyFileType = chkDenyFileType.Checked;
            CheckIsRule();
        }

        private void chkDenyIPAddress_Click(object sender, EventArgs e)
        {
            if (chkDenyIPAddress.Checked)
            {
                ClearOtherAllowCheckBox(ref chkDenyIPAddress);
            }
            Properties.Settings.Default.isDenyIPAddress = chkDenyIPAddress.Checked;
            CheckIsRule();
        }

        private void chkDenyWebsite_Click(object sender, EventArgs e)
        {
            if (chkDenyWebsite.Checked)
            {
                ClearOtherAllowCheckBox(ref chkDenyWebsite);
            }
            Properties.Settings.Default.isDenyWebsite = chkDenyWebsite.Checked;
            CheckIsRule();
        }

        private void chkDenyFileTypeIPAddress_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.isIPAddressDenyFileType = chkDenyFileTypeIPAddress.Checked;
            CheckIsRule();
        }

        private void chkDenyFileTypeDomain_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.isWebsiteDenyFileType = chkDenyFileTypeDomain.Checked;
            CheckIsRule();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                GlobalVariables.listPriorityRule[(int)GlobalVariables.RuleType.AllowFileType - 1] = Int32.Parse(txtAllowFileType.Text);
                GlobalVariables.listPriorityRule[(int)GlobalVariables.RuleType.AllowIPAddress - 1] = Int32.Parse(txtAllowIPAddress.Text);
                GlobalVariables.listPriorityRule[(int)GlobalVariables.RuleType.AllowWebsite - 1] = Int32.Parse(txtAllowWebsite.Text);
                GlobalVariables.listPriorityRule[(int)GlobalVariables.RuleType.DenyFileType - 1] = Int32.Parse(txtDenyFileType.Text);
                GlobalVariables.listPriorityRule[(int)GlobalVariables.RuleType.DenyIPAddress - 1] = Int32.Parse(txtDenyIPAddress.Text);
                GlobalVariables.listPriorityRule[(int)GlobalVariables.RuleType.DenyWebsite - 1] = Int32.Parse(txtDenyWebsite.Text);
                GlobalVariables.listPriorityRule[(int)GlobalVariables.RuleType.DenyDomainFileType - 1] = Int32.Parse(txtWebsiteDenyFileType.Text);
                GlobalVariables.listPriorityRule[(int)GlobalVariables.RuleType.DenyIPAddressFileType - 1] = Int32.Parse(txtIPAddressDenyFileType.Text);
            }
            catch (Exception ex)
            {
            }
        }

        private void lnkDenyWebsite_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            panelRule.Controls.Clear();
            ucPrivacyDomain ucDomain = new ucPrivacyDomain(true);
            panelRule.Controls.Add(ucDomain);

        }

        private void lnkDenyWebsiteFileType_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            panelRule.Controls.Clear();
            ucWebsiteDenyFileType ucWebsiteDenyFileType = new ucWebsiteDenyFileType();
            panelRule.Controls.Add(ucWebsiteDenyFileType);
        }

        private void lnkDenyIPAddress_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            panelRule.Controls.Clear();
            ucPrivacyIPAddress ucIPAddress = new ucPrivacyIPAddress(true);
            panelRule.Controls.Add(ucIPAddress);
        }

        private void lbnDenyFileType_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            panelRule.Controls.Clear();
            ucPrivacyFileTypes ucFileType = new ucPrivacyFileTypes(true);
            panelRule.Controls.Add(ucFileType);
        }

        private void lnkAllowWebsite_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            panelRule.Controls.Clear();
            ucPrivacyDomain ucDomain = new ucPrivacyDomain(false);
            panelRule.Controls.Add(ucDomain);
        }

        private void lnkAllowIPAddress_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            panelRule.Controls.Clear();
            ucPrivacyIPAddress ucIPAddress = new ucPrivacyIPAddress(false);
            panelRule.Controls.Add(ucIPAddress);
        }

        private void lnkDenyIPAddressFileType_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            panelRule.Controls.Clear();
            ucIPAddressDenyFileTypes ucAddressDenyFileType = new ucIPAddressDenyFileTypes();
            panelRule.Controls.Add(ucAddressDenyFileType);
        }

        private void lnkAllowFileTypes_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            panelRule.Controls.Clear();
            ucPrivacyFileTypes ucFileType = new ucPrivacyFileTypes(false);
            panelRule.Controls.Add(ucFileType);
        }
    }
}
